<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        canvas{
            border: 1px solid black;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas width="400px" height="500px" id="myCanvas">
    </canvas>    
    <!-- 画布的起始点是左上角（0,0） -->
    <script>
        //获取画布
        //获取画笔
        var paint = myCanvas.getContext('2d');
        myCanvas.onmousedown = function(e){
            paint.beginPath();
            var x = e.pageX - myCanvas.offsetLeft;
            var y = e.pageY - myCanvas.offsetTop;
            paint.moveTo(x,y);
            document.onmousemove = function(e){
                var x = e.pageX - myCanvas.offsetLeft;
                var y = e.pageY - myCanvas.offsetTop;
                paint.lineTo(x,y);
                paint.stroke();
            }
            document.onmouseup = function(){
                document.onmousemove = null;
            }
        }       
        



    </script>
</body>
</html>